package cn.tedu.bailianagent.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/ai")
public class BaiLianAgentController {

    // 声明DashScopeAgent对象，用于与阿里百炼服务交互
    private DashScopeAgent agent;

    // @Value从配置文件中注入spring.ai.dashscope.agent.options.app-id的值
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String appId;

    // 构造器注入DashScopeAgentApi对象
    public BaiLianAgentController(DashScopeAgentApi dashScopeAgentApi) {
        // 使用注入的dashscopeAgentApi创建DashScopeAgent实例
        this.agent = new DashScopeAgent(dashScopeAgentApi);
    }


    @GetMapping("/bailian/agent/call")
    public String call(String message){
        log.debug("百炼调用业务:message={}",message);
        log.debug("appId={}",appId);
        ChatResponse response = agent.call(new Prompt(message,
                DashScopeAgentOptions.builder().withAppId(appId).build()));
        log.debug("阿里百炼大模型返回结果:response={}",response);
        String content = response.getResult().getOutput().getText();
        log.debug("阿里百炼大模型返回结果:content={}",content);
        return content;
    }
}
